lazy-cache
Cache requires to be lazy-loaded when needed.
Install
Install with npm:
$ npm install --save lazy-cache
Heads up!
It's suprising how many libraries are in the average dependency tree that don't belong there for one reason or another. Either because they were accidentally listed as dependencies
instead of devDepedencies
, or they are required in a file as variables, but the variable is never actually used (poor linting), and so on. Or because the maintainer made the decision to add the deps, even though they will never (or can't ever) be used by 99.9% of users.
Worse, many libraries like chalk and shelljs actually execute code when require()
is called!? (shelljs was modifying the String.prototype
, and chalk loops over some objects to dynamically create methods). In other words, they do something like this:
String.prototype.foo = function() {};
while (foo) {
}
In any case, just having these libraries in your dependency tree somewhere means that their code will excecute the moment you run your application even if the libraries are never called by your application or any other code anywhere in the tree.
solution
lazy-cache doesn't use any "magic", it uses native, plain-vanilla, tried and true javascript getters to call node's require()
system.
Faster, safer code
There main advantage to this, the main is that require
s are loaded on demand, so only code that is actually used will ever be loaded. As a result, applications will load faster (sometimes much faster - we've seen load times drop from ~1 second to less than 50 milliseconds).
Moreover, in some cases this also avoids inadvertently loading libraries that execute code or modifies globals, etc.
webpack users
If you use webpack and are experiencing issues with lazy-cache, this is a known bug caused by webpack, not lazy-cache. There is a solution though, you can use unlazy-loader, a webpack loader that fixes the webpack bug.
Usage
var utils = require('lazy-cache')(require);
Use as a property on lazy
The module is also added as a property to the lazy
function so it can be called without having to call a function first.
var utils = require('lazy-cache')(require);
utils('glob');
console.log(utils.glob.sync('*.js'));
utils.glob('*.js', function (err, files) {
console.log(files);
});
Use as a function
var utils = require('lazy-cache')(require);
var glob = utils('glob');
glob().sync('foo/*.js');
Aliases
An alias may be passed as the second argument if you don't want to use the automatically camel-cased variable name.
Example
var utils = require('lazy-cache')(require);
utils('ansi-yellow', 'yellow');
console.log(utils.yellow('foo'));
Dot notation may also be used in the alias to create an object hierarchy.
Example
var utils = require('lazy-cache')(require);
utils('ansi-cyan', 'color.cyan');
utils('ansi-yellow', 'color.yellow');
utils('ansi-magenta', 'color.magenta');
console.log(utils.color.cyan('foo'));
console.log(utils.color.yellow('bar'));
console.log(utils.color.magenta('baz'));
Browserify usage
Example
var utils = require('lazy-cache')(require);
var fn = require;
require = utils;
require('glob');
require = fn;
console.log(utils.glob.sync('*.js'));
utils.glob('*.js', function (err, files) {
console.log(files.join('\n'));
});
Kill switch
To force lazy-cache to immediately invoke all dependencies, do:
process.env.UNLAZY = true;
About
Related projects
lint-deps: CLI tool that tells you when dependencies are missing from package.json and offers you a… more | homepage
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Contributors
Building docs
(This document was generated by verb-generate-readme (a verb generator), please don't edit the readme directly. Any changes to the readme must be made in .verb.md.)
To generate the readme and API documentation with verb:
$ npm install -g verb verb-generate-readme && verb
Running tests
Install dev dependencies:
$ npm install -d && npm test
Author
Jon Schlinkert
License
Copyright © 2016, Jon Schlinkert.
Released under the MIT license.
This file was generated by verb-generate-readme, v0.2.0, on November 07, 2016.